package com.key.org.service.serviceImp;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;

import com.key.common.model.SelData;
import com.key.common.service.IDataListService;
import com.key.org.model.Department;
import com.key.util.HibernateUtil;

public class GetDepartmentServiceImp implements IDataListService {

	@Override
	public List<SelData> getData(HttpServletRequest request, Object keyword) {
		// TODO Auto-generated method stub
		List<SelData> list = new ArrayList<SelData>();
		String keywords = null;
		if (keyword != null) {
			keywords = keyword.toString();
			request.setAttribute("keyword", keywords);
		}
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			String hql = "from Department department";
			if (keywords != null) {
				hql += " where department.name like '%" + keywords + "%'";
			}
			List<Department> departmentList = session.createQuery(hql).list();
			for (int i = 0; i < departmentList.size(); i++) {
				Department department = departmentList.get(i);
				SelData selData = new SelData();
				selData.setId(department.getId());
				selData.setDisplayName(department.getName());
				selData.setOtherInfo("");
				list.add(selData);
			}
			session.getTransaction().commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		return list;
	}
}
